gdk: Add a gl texture implementation
authorMatthias Clasen <mclasen@redhat.com>
Wed, 17 Jan 2018 05:32:26 +0000 (00:32 -0500)
committerMatthias Clasen <mclasen@redhat.com>
Wed, 17 Jan 2018 16:56:47 +0000 (11:56 -0500)
This will be used to pass a GL textures from the application
(or rather, GtkGLArea) down to the GSK GL renderer.

gdk/gdktexture.c
gdk/gdktexture.h
gdk/gdktextureprivate.h

index 936b5c76411ba28a7acc39522436fff8fa3946cb..c87ed57a1d1d3974c1c4b63f598f05bb23cecaf5 100644 (file)
@@ -39,6 +39,8 @@
 #include "gdkinternals.h"
 #include "gdkcairo.h"
 
+#include <epoxy/gl.h>
+
 /**
  * SECTION:gdktexture
  * @Short_description: Image data for display
@@ -432,6 +434,88 @@ gdk_pixbuf_texture_init (GdkPixbufTexture *self)
 {
 }
 
+/* GdkGLTexture */
+
+
+struct _GdkGLTexture {
+  GdkTexture parent_instance;
+
+  GdkGLContext *context;
+  int id;
+
+  GDestroyNotify destroy;
+  gpointer data;
+};
+
+struct _GdkGLTextureClass {
+  GdkTextureClass parent_class;
+};
+
+G_DEFINE_TYPE (GdkGLTexture, gdk_gl_texture, GDK_TYPE_TEXTURE)
+
+static void
+gdk_gl_texture_dispose (GObject *object)
+{
+  GdkGLTexture *self = GDK_GL_TEXTURE (object);
+
+  g_object_unref (self->context);
+
+  if (self->destroy)
+    self->destroy (self->data);
+
+  G_OBJECT_CLASS (gdk_gl_texture_parent_class)->dispose (object);
+}
+
+static void
+gdk_gl_texture_download (GdkTexture *texture,
+                         guchar     *data,
+                         gsize       stride)
+{
+  GdkGLTexture *self = GDK_GL_TEXTURE (texture);
+  cairo_surface_t *surface;
+  cairo_t *cr;
+  GdkWindow *window;
+
+  surface = cairo_image_surface_create_for_data (data,
+                                                 CAIRO_FORMAT_ARGB32,
+                                                 texture->width, texture->height,
+                                                 stride);
+
+  cr = cairo_create (surface);
+  window = gdk_gl_context_get_window (self->context);
+  gdk_cairo_draw_from_gl (cr, window, self->id, GL_TEXTURE, 1, 0, 0, texture->width, texture->height);
+  cairo_destroy (cr);
+  cairo_surface_finish (surface);
+  cairo_surface_destroy (surface);
+}
+
+static void
+gdk_gl_texture_class_init (GdkGLTextureClass *klass)
+{
+  GdkTextureClass *texture_class = GDK_TEXTURE_CLASS (klass);
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+  texture_class->download = gdk_gl_texture_download;
+  gobject_class->dispose = gdk_gl_texture_dispose;
+}
+
+static void
+gdk_gl_texture_init (GdkGLTexture *self)
+{
+}
+
+GdkGLContext *
+gdk_gl_texture_get_context (GdkGLTexture *self)
+{
+  return self->context;
+}
+
+int
+gdk_gl_texture_get_id (GdkGLTexture *self)
+{
+  return self->id;
+}
+
 /**
  * gdk_texture_new_for_pixbuf:
  * @pixbuf: a #GdkPixbuf
@@ -532,6 +616,31 @@ gdk_texture_new_from_file (GFile   *file,
   return texture;
 }
 
+GdkTexture *
+gdk_texture_new_for_gl (GdkGLContext   *context,
+                        int             id,
+                        int             width,
+                        int             height,
+                        GDestroyNotify  destroy,
+                        gpointer        data)
+{
+  GdkGLTexture *self;
+
+  g_return_val_if_fail (GDK_IS_GL_CONTEXT (context), NULL);
+
+  self = g_object_new (GDK_TYPE_GL_TEXTURE,
+                       "width", width,
+                       "height", height,
+                       NULL);
+
+  self->context = g_object_ref (context);
+  self->id = id;
+  self->destroy = destroy;
+  self->data = data;
+
+  return GDK_TEXTURE (self);
+}
+
 /**
  * gdk_texture_get_width:
  * @texture: a #GdkTexture
index fe957c09159356d361c5fc8cd3dd80bcbfe52860..4f3ead2321245a5b88407955670d2008a28b9811 100644 (file)
@@ -26,6 +26,7 @@
 #include <gdk/gdkversionmacros.h>
 #include <gdk/gdktypes.h>
 #include <gdk-pixbuf/gdk-pixbuf.h>
+#include <gdk/gdkglcontext.h>
 
 G_BEGIN_DECLS
 
@@ -55,6 +56,14 @@ GDK_AVAILABLE_IN_3_94
 GdkTexture *            gdk_texture_new_from_file              (GFile           *file,
                                                                 GError         **error);
 
+GDK_AVAILABLE_IN_3_94
+GdkTexture *            gdk_texture_new_for_gl                 (GdkGLContext    *context,
+                                                                int              id,
+                                                                int              width,
+                                                                int              height,
+                                                                GDestroyNotify   destroy,
+                                                                gpointer         data);
+
 GDK_AVAILABLE_IN_3_94
 int                     gdk_texture_get_width                  (GdkTexture      *texture);
 GDK_AVAILABLE_IN_3_94
index f9e5fbe908ee1efdb0f94fcd1f687dac146501b1..6aa96d16c30e5ac0180a700057102ebd3194e24c 100644 (file)
@@ -44,6 +44,13 @@ void                    gdk_texture_clear_render_data   (GdkTexture
 gpointer                gdk_texture_get_render_data     (GdkTexture             *self,
                                                          gpointer                key);
 
+#define GDK_TYPE_GL_TEXTURE (gdk_gl_texture_get_type ())
+
+G_DECLARE_FINAL_TYPE (GdkGLTexture, gdk_gl_texture, GDK, GL_TEXTURE, GdkTexture)
+
+GdkGLContext *          gdk_gl_texture_get_context      (GdkGLTexture           *self);
+int                     gdk_gl_texture_get_id           (GdkGLTexture           *self);
+
 G_END_DECLS
 
 #endif /* __GDK_TEXTURE_PRIVATE_H__ */